package com.jvm123.blog.interceptor

import com.jvm123.blog.service.AccessService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @author yawn http://jvm123.com
 *     2019/10/18 16:29
 */
@Component
class LoginInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    AccessService accessService

    @Override
    boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI()
        String role = accessService.getRole(request)
        boolean result = accessService.checkRule(uri, role)
        if (!result) {
            response.sendError(404)
        }
        return result
    }

}
